package cn.kgc.springcloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springcloud.entity.Storage;
import cn.kgc.springcloud.service.StorageService;
import cn.kgc.springcloud.mapper.StorageMapper;
import org.springframework.stereotype.Service;

/**
* @author mengshun
* @description 针对表【storage_tbl】的数据库操作Service实现
* @createDate 2025-01-16 09:00:22
*/
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage>implements StorageService{

    @Override
    public void deduct(String commodityCode, Integer count) {

        // 判定库存是否充足
        LambdaQueryWrapper<Storage> lambda = new QueryWrapper<Storage>().lambda();
        lambda.eq(Storage::getCommodityCode,commodityCode);

        Integer DBCount = baseMapper.selectOne(lambda).getCount();

        if (count > DBCount){
            throw  new RuntimeException("库存不足");
        }

        // 根据商品编号 扣减库存    update storage  set count = count - 5  where  commodityCode = 1001
        LambdaUpdateWrapper<Storage> lambda1 = new UpdateWrapper<Storage>().lambda();
        lambda1.setSql("count = count - "+count)
                .eq(Storage::getCommodityCode,commodityCode);

        baseMapper.update(lambda1);

    }
}




